package com.ccrfid.rmc6.resource.personnel;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.personnel.*;
import com.ccrfid.rmc6.pojo.dto.personnel.PersonnelVitalSignConfigDeleteVo;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.auth.pojo.UserPrincipal;
import com.ccrfid.rmc6.service.personnel.PersonnelVitalSignAlarmService;
import com.ccrfid.rmc6.service.personnel.PersonnelVitalSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Secured
@Path("vital-sign")
@Produces(MediaType.APPLICATION_JSON)
public class PersonnelVitalSignResource {

    @Context
    private SecurityContext securityContext;
    @Autowired
    private PersonnelVitalSignService personnelVitalSignService;
    @Autowired
    private PersonnelVitalSignAlarmService personnelVitalSignAlarmService;

    //获取所有生命体征关注人员
    @GET
    @Path("focus-personnels")
    public List<PersonnelView> getAllPersonnelsWithVitalSignFocus() {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        return personnelVitalSignService.getAllPersonnelsWithVitalSignFocus(userPrincipal.getUserAccount());
    }

    //添加某个生命体征关注人员
    @POST
    @Path("focus-personnels/{personnelId}")
    public Boolean createVitalSignFocusPersonnel(@PathParam("personnelId") Long personnelId) throws AppException {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        return personnelVitalSignService.createVitalSignFocusPersonnel(userPrincipal.getUserAccount(), personnelId);
    }

    //删除某个生命体征关注人员
    @DELETE
    @Path("focus-personnels/{personnelId}")
    public Boolean deleteVitalSignFocusPersonnel(@PathParam("personnelId") Long personnelId) throws AppException {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        return personnelVitalSignService.deleteVitalSignFocusPersonnel(userPrincipal.getUserAccount(), personnelId);
    }

    //获取某个人某段时间计步数
    @GET
    @Path("personnels/{personnelId}/step-count")
    public Integer getPersonnelStepCount(@PathParam("personnelId") Long personnelId,
                                         @QueryParam("startTime") Timestamp startTime,
                                         @QueryParam("stopTime") Timestamp stopTime) throws AppException {
        if (startTime != null && stopTime != null) {
            return personnelVitalSignService.getPersonnelStepCount(personnelId, startTime, stopTime);
        } else {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
    }

    //获取某个人某段时间心率记录
    @GET
    @Path("personnels/{personnelId}/heart-rate-records")
    public List<PersonnelHeartRateEntity> getPersonnelHeartRateRecords(@PathParam("personnelId") Long personnelId,
                                                                       @QueryParam("startTime") Timestamp startTime,
                                                                       @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        if (startTime != null && stopTime != null) {
            return personnelVitalSignService.getPersonnelHeartRateRecords(personnelId, startTime, stopTime);
        } else {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
    }

    //获取某个时间心率有异常的人员列表
    @GET
    @Path("personnels-with-abnormal-heart-rate")
    public List<PersonnelView> getAllPersonnelsWithAbnormalHeartRate(@QueryParam("checkTime") Timestamp checkTime) {
        if (checkTime == null) {
            checkTime = new Timestamp(System.currentTimeMillis());
        }
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();

        return personnelVitalSignService.getAllPersonnelsWithAbnormalHeartRate(userPrincipal.getOrgId(), checkTime);
    }

    //获取心率正常值范围
    @GET
    @Path("normal-heart-rate-value-range")
    public Map<String, Integer> getNormalHeartRateValueRange() {
        return personnelVitalSignService.getNormalHeartRateValueRange();
    }

    //8.获取某人最新心率数据
    @GET
    @Path("personnel/{personnelId}/data/recent-heart-rate")
    public PersonnelHeartRateEntity getPersonnelRecentHeartRate(@PathParam("personnelId") Long personnelId)
            throws AppException {
        if (personnelId == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelRecentHeartRate(personnelId);
    }

    //9.获取某人某段时间内的心率数据
    @GET
    @Path("personnel/{personnelId}/data/heart-rate-list")
    public List<PersonnelHeartRateEntity> getPersonnelHeartRateList(@PathParam("personnelId") Long personnelId,
                                                                    @QueryParam("startTime") Timestamp startTime,
                                                                    @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        if (personnelId == null || startTime == null || stopTime == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelHeartRateList(personnelId, startTime, stopTime);
    }

    //10.获取某人某段时间内的计步数据
    @GET
    @Path("personnel/{personnelId}/data/step-count")
    public Integer getPersonnelStepCountData(@PathParam("personnelId") Long personnelId,
                                             @QueryParam("startTime") Timestamp startTime,
                                             @QueryParam("stopTime") Timestamp stopTime) throws AppException {
        if (personnelId == null || startTime == null || stopTime == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelStepCountData(personnelId, startTime, stopTime);
    }

    //11.获取某人最新体温数据
    @GET
    @Path("personnel/{personnelId}/data/recent-temperature")
    public PersonnelTemperatureEntity getPersonnelRecentTemperature(@PathParam("personnelId") Long personnelId)
            throws AppException {
        if (personnelId == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelRecentTemperature(personnelId);
    }

    //12.获取某人某段时间内的体温数据
    @GET
    @Path("personnel/{personnelId}/data/temperature-list")
    public List<PersonnelTemperatureEntity> getPersonnelTemperatureList(@PathParam("personnelId") Long personnelId,
                                                                        @QueryParam("startTime") Timestamp startTime,
                                                                        @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        if (personnelId == null || startTime == null || stopTime == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelTemperatureList(personnelId, startTime, stopTime);
    }

    //13.获取某人最新血压数据
    @GET
    @Path("personnel/{personnelId}/data/recent-blood-pressure")
    public PersonnelBloodPressureEntity getPersonnelRecentBloodPressure(@PathParam("personnelId") Long personnelId)
            throws AppException {
        if (personnelId == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelRecentBloodPressure(personnelId);
    }

    //14.获取某人某段时间内的血压数据
    @GET
    @Path("personnel/{personnelId}/data/blood-pressure-list")
    public List<PersonnelBloodPressureEntity> getPersonnelBloodPressureList(@PathParam("personnelId") Long personnelId,
                                                                            @QueryParam("startTime") Timestamp startTime,
                                                                            @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        if (personnelId == null || startTime == null || stopTime == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return personnelVitalSignService.getPersonnelBloodPressureList(personnelId, startTime, stopTime);
    }

    //获取人员生命体征报警配置
    @GET
    @Path("get-personnel-vital-sign-alarm-config")
    public Page<PersonnelVitalSignsAlarmConfigEntity> getPersonnelVitalSignAlarmConfigPage(@QueryParam("personnelId") Long personnelId,
                                                                                           @QueryParam("personnelName")String personnelName,
                                                                                           @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (personnelId != null) {
            criteria.add(new Criterion("personnelId", Criterion.Operator.EQ, personnelId));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, personnelName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return personnelVitalSignAlarmService .getPersonnelVitalSignAlarmConfig(criteria, pageRequest);
    }

    //新增某个人员生命体征报警配置
    @POST
    @Path("add-personnel-vital-sign-alarm-config")
    @Consumes(MediaType.APPLICATION_JSON)
    public PersonnelVitalSignsAlarmConfigEntity createPersonnelVitalSignAlarmConfig(PersonnelVitalSignsAlarmConfigEntity personnelVitalSignsAlarmConfigEntity) throws AppException {
        return personnelVitalSignAlarmService.addPersonnelVitalSignAlarmConfig(personnelVitalSignsAlarmConfigEntity);
    }

    //修改某个人员生命体征报警配置
    @PUT
    @Path("update-personnel-vital-sign-alarm-config")
    @Consumes(MediaType.APPLICATION_JSON)
    public PersonnelVitalSignsAlarmConfigEntity updatePersonnelVitalSignAlarmConfig(PersonnelVitalSignsAlarmConfigEntity entity) throws AppException {
        return personnelVitalSignAlarmService.updatePersonnelVitalSignAlarmConfig(entity);
    }

    //重置人员生命体征报警配置
    @Path("reset-personnel-vital-sign-alarm-config")
    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean resetPersonnelVitalSignAlarmConfig(PersonnelVitalSignConfigDeleteVo idsList) throws AppException {
        return personnelVitalSignAlarmService.deletePersonnelVitalSignAlarmConfig(idsList.getIdsList());
    }

    //获取人员生命体征报警配置
    @GET
    @Path("get-single-personnel-vital-sign-alarm-config")
    public PersonnelVitalSignsAlarmConfigEntity getPersonnelVitalSignAlarmConfig(@QueryParam("personnelId") Long personnelId) throws AppException {
        return personnelVitalSignAlarmService .getSinglePersonnelVitalSignAlarmConfig(personnelId);
    }



}
